/**
 * Created with JetBrains WebStorm.
 * User: Fred_974
 * Date: 04/06/13
 * Time: 15:55
 * To change this template use File | Settings | File Templates.
 */

var Weapon = require("../Model/Weapon");
var weaponDao = require("../model/WeaponDao");
var Plantation = require("../Model/Plantation");
var plantationDao = require("../model/PlantationDao");
var Maturity = require("../Model/Maturity");
var maturityDao = require("../model/MaturityDao");// We use the Dao
var forms = require("../form/administrationForm");



exports.editWeapon = function(req, res){

    var id = req.params.id;

    weaponDao.findById(id,function(weapon){

        forms.weaponForm.handle(req, {

            success:function (form) {



                var weapon = new Weapon();
                if (form.data.id != null && form.data.id != "" && form.data.id != undefined)
                    weapon.id = form.data.id;

                weapon.name = form.data.name;
                weapon.requirement = form.data.requirement;
                weapon.cadence = form.data.cadence;
                weapon.damage = form.data.damage;
                weapon.precision = form.data.precision;
                weapon.price = form.data.price;


                console.log("methode administration exports: " + form.data.Money);

                weaponDao.persistWeaponSettings(weapon,
                    function (weapon) {

                        res.redirect("/administration");

                    });

            },



            other:function (form) {
                form = form.bind(weapon);
                res.render("weaponEdit", { administrationForm:form});
            }



        });



    });




},

exports.editMaturity = function(req, res){

        var id = req.params.id;

        maturityDao.findById(id,function(maturity){

            forms.maturityForm.handle(req, {

                success:function (form) {



                    var maturity = new Maturity();
                    if (form.data.id != null && form.data.id != "" && form.data.id != undefined)
                        maturity.id = form.data.id;

                    maturity.name = form.data.name;
                    maturity.min = form.data.min;
                    maturity.max = form.data.max;




                    maturityDao.persistMaturitySettings(maturity,
                        function (maturity) {

                            res.redirect("/administration");

                        });

                },



                other:function (form) {
                    form = form.bind(maturity);
                    res.render("maturityEdit", { administrationForm:form});
                }



            });



        });




},

exports.index = function(req,res){

    weaponDao.findAllWeapons(function(weapons){

        res.render("weaponIndex", {administrationWeapons:weapons});

})
}

exports.plantationIndex = function(req,res){



        res.render("plantationIndex");


}

exports.maturityIndex = function(req,res){


    maturityDao.findAllMaturity(function(maturity){
    res.render("maturityIndex", {administrationMaturity:maturity});

   })

}



exports.deleteWeapon = function(req,res){

    var id = req.params.id;

    weaponDao.findById(id,function(weapon){

        forms.weaponForm.handle(req, {

            success:function (form) {



                var weapon = new Weapon();
                if (form.data.id != null && form.data.id != "" && form.data.id != undefined)
                    weapon.id = form.data.id;

                weapon.name = form.data.name;
                weapon.requirement = form.data.requirement;
                weapon.cadence = form.data.cadence;
                weapon.damage = form.data.damage;
                weapon.precision = form.data.precision;
                weapon.price = form.data.price;




                weaponDao.deleteWeapon(weapon,
                    function (weapon) {

                        res.redirect("/administration");

                    });

            },



            other:function (form) {
                form = form.bind(weapon);
                res.render("weaponDelete", { administrationWeaponDelete:form});
            }



        });



    });

}

    exports.createWeapon = function(req, res){


        var weapon;

            forms.weaponForm.handle(req, {

                success:function (form) {



                    weapon = new Weapon();
                 if (form.data.id != null && form.data.id != "" && form.data.id != undefined)
                   //     weapon.id = form.data.id;


                    console.log(weapon.name);
                    weapon.name = form.data.name;
                    console.log(weapon.name);
                    weapon.requirement = form.data.requirement;
                    weapon.cadence = form.data.cadence;
                    weapon.damage = form.data.damage;
                    weapon.precision = form.data.precision;
                    weapon.price = form.data.price;




                    weaponDao.persistWeaponSettings(weapon,
                        function (weapon) {

                            res.redirect("/administration");

                        });

                },



                other:function (form) {

                    res.render("weaponCreate", { administrationCreateW:form});
                }



            });



    };

exports.createPlantation = function(req, res){


    var plantation;


    forms.plantationForm.handle(req, {

        success:function (form) {



            plantation = new Plantation();





            plantation.type = form.data.type;
            plantation.maturationTime = form.data.maturationTime;
            plantation.growRate = form.data.growRate;
            plantation.decayTime = form.data.decayTime;
            plantation.seedPrice = form.data.seedPrice;





            plantationDao.persistPlantationSettings(plantation,
                function (plantation) {

                    res.redirect("/administration");

                });



        },



        other:function (form) {

            res.render("plantationCreate", { administrationCreateP:form});
        }



    });



};

exports.createMaturity = function(req, res){


    var maturity;


    forms.maturityForm.handle(req, {

        success:function (form) {



            maturity = new Maturity();





            maturity.name = form.data.name;
            maturity.min = form.data.min;
            maturity.max = form.data.max;






            maturityDao.persistMaturitySettings(maturity,
                function (maturity) {

                    res.redirect("/administration");

                });



        },



        other:function (form) {

            res.render("maturityCreate", { administrationCreateM:form});
        }



    });



};

exports.deleteMaturity = function(req,res){

    var id = req.params.id;

    maturityDao.findById(id,function(maturity){

        forms.maturityForm.handle(req, {

            success:function (form) {



                var maturity = new Maturity();
                if (form.data.id != null && form.data.id != "" && form.data.id != undefined)
                    maturity.id = form.data.id;

                maturity.name = form.data.name;
                maturity.min = form.data.min;
                maturity.max = form.data.max;





                maturityDao.deleteMaturity(maturity,
                    function (maturity) {

                        res.redirect("/administration");

                    });

            },



            other:function (form) {
                form = form.bind(maturity);
                res.render("maturityDelete", { administrationMaturityDelete:form});
            }



        });



});

}








